
package brand

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/brand"
    brandReq "github.com/flipped-aurora/gin-vue-admin/server/model/brand/request"
)

type BrandService struct {}
// CreateBrand 创建品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService) CreateBrand(ctx context.Context, brands *brand.Brand) (err error) {
	err = global.GVA_DB.Create(brands).Error
	return err
}

// DeleteBrand 删除品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService)DeleteBrand(ctx context.Context, brandId string) (err error) {
	err = global.GVA_DB.Delete(&brand.Brand{},"brand_id = ?",brandId).Error
	return err
}

// DeleteBrandByIds 批量删除品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService)DeleteBrandByIds(ctx context.Context, brandIds []string) (err error) {
	err = global.GVA_DB.Delete(&[]brand.Brand{},"brand_id in ?",brandIds).Error
	return err
}

// UpdateBrand 更新品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService)UpdateBrand(ctx context.Context, brands brand.Brand) (err error) {
	err = global.GVA_DB.Model(&brand.Brand{}).Where("brand_id = ?",brands.BrandId).Updates(&brands).Error
	return err
}

// GetBrand 根据brandId获取品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService)GetBrand(ctx context.Context, brandId string) (brands brand.Brand, err error) {
	err = global.GVA_DB.Where("brand_id = ?", brandId).First(&brands).Error
	return
}
// GetBrandInfoList 分页获取品牌管理记录
// Author [yourname](https://github.com/yourname)
func (brandsService *BrandService)GetBrandInfoList(ctx context.Context, info brandReq.BrandSearch) (list []brand.Brand, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&brand.Brand{})
    var brandss []brand.Brand
    // 如果有条件搜索 下方会自动创建搜索语句
    
    if info.BrandName != nil && *info.BrandName != "" {
        db = db.Where("brand_name LIKE ?", "%"+ *info.BrandName+"%")
    }
    if info.Status != nil && *info.Status != "" {
        db = db.Where("status = ?", *info.Status)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&brandss).Error
	return  brandss, total, err
}
func (brandsService *BrandService)GetBrandPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
